<?php
if ( !defined("SCRIPT_DIR") ) {
	define("SCRIPT_DIR", dirname(__FILE__));
}
$path = explode('/',__FILE__);

if ( !defined("FILE") ) {
	define("FILE",end($path));
}

function path_format($file) {
	return SCRIPT_DIR . '/' . $file;
}

include_once(path_format('lib/logger.php'));
include_once(path_format('config/config.php'));
include_once(path_format('lib/mysql.php'));
include_once(path_format('lib/redis.php'));
include_once(path_format('lib/httpclient.php'));
include_once(path_format('lib/function.php'));

class SyncConf {
	public static $sync_sec = 250;
}

function sync(){
	$redis = new RedisDriver();
    $ret = $redis->initDataRedis();
    if (!$ret) {
	    Config::$loggerError->error(FILE, "initDataRedis error");
    }

	$mysql = new MysqlDriver ();
	$rets = $mysql->connect ( Config::$mysql_config );
	if (! $rets) {
		Config::$loggerError->error ( FILE, "connect mysql error." );
	}

    $redis->data_redis->select(1);
	$hSessionID_UserID = $redis->data_redis->hgetAll("session:hUserID_SessionID");//在线人数
	$hUserDesk = $redis->data_redis->hgetAll("hUserDesk");//在玩人数
	
	//上报在线在玩人数数据
	$param = array('cid'=>Config::$info_report['cid'],'record_time'=>time(),'ou'=>(int) count($hSessionID_UserID),
	'opu'=>(int) count($hUserDesk));
	
	$redis->data_redis->select(3);
	$redis->data_redis->rpush('online_data',json_encode($param));

    $redis->deinitDataRedis ();
	$mysql->close ();
}

function main() {
		while(1) {
		sync();	
		echo date('Y:m:d H:i:s')." ZjhSyncDumpOnlineData working...\r\n"; 	
		$sync_sec = SyncConf::$sync_sec;
		sleep($sync_sec);
		
	   }
}

main ();


?>